package com.gimi.cloud.bbp.app.consumer.service.product.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gimi.cloud.bbp.dto.prod.EcProdCategoryDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gimi.cloud.bbp.app.consumer.feign.product.ProdCategoryFeignService;
import com.gimi.cloud.bbp.app.consumer.service.product.CategoryService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;

@Service
public class CategoryServiceImpl implements CategoryService {
    private static final Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
    private final ProdCategoryFeignService prodCategoryFeignService;

    @Autowired
    public CategoryServiceImpl(ProdCategoryFeignService prodCategoryFeignService){
        this.prodCategoryFeignService = prodCategoryFeignService;
    }
    
    @Override
	public void firstGradeCategories(HttpServletRequest request, HttpServletResponse response) {
	        Util.sendResponse(prodCategoryFeignService.firstGradeCategories(), response);
	}

	@Override
	public void secondGradeCategories(String requestData, HttpServletRequest request, HttpServletResponse response) {
		if(StringUtil.isBlank(requestData)){
			logger.error("请求参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestData));
        JSONObject json = null;
        EcProdCategoryDTO prodCategoryDTO = new EcProdCategoryDTO();
        try{
        	json=JSON.parseObject(requestData);
        	prodCategoryDTO.setParentCatId(json.getString("parentCatId"));
        }catch(Exception e){
        	logger.error("传入的参数格式错误："+e.getMessage());
        	throw new MsgException(ResponseCodeEnum.PARAMETER_EXCEPTION);
        }
        if(StringUtil.isBlank(prodCategoryDTO.getParentCatId())){
        	throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(prodCategoryFeignService.secondGradeCategories(prodCategoryDTO), response);
	}
    
}
